
// Copyright (C) 2009-2011 Luca Piccioni
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//  
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Windows.Forms;

namespace Derm.Ui
{
	/// <summary>
	/// 
	/// </summary>
	public interface IKeyboardInput
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void KeyDownEvent(object sender, System.Windows.Forms.KeyEventArgs e);

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void KeyUpEvent(object sender, System.Windows.Forms.KeyEventArgs e);
	}

	/// <summary>
	/// Keyboard state abstraction.
	/// </summary>
	public abstract class KeyboardInput : IKeyboardInput
	{
		#region Keyboard State

		/// <summary>
		/// Keyboard key pressed state.
		/// </summary>
		private Keys mKeys = 0;

		#endregion

		#region Keyboard Events

		/// <summary>
		/// 
		/// </summary>
		/// <param name="key"></param>
		public abstract void KeyPressed(System.Windows.Forms.Keys key);

		#endregion

		#region IKeyboardInput Implementation

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void KeyDownEvent(object sender, KeyEventArgs e)
		{
			// Set pressed key mask
			mKeys |=  (e.KeyCode & Keys.KeyCode);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void KeyUpEvent(object sender, KeyEventArgs e)
		{
			Keys sKey = e.KeyCode & Keys.KeyCode;

			// Unset pressed key mask
			mKeys &= ~sKey;
			// Notify about pressed key
			KeyPressed(sKey);
		}

		#endregion
	}
}
